package com.hanthink.web.controller.app.report;

import com.hanthink.common.annotation.Log;
import com.hanthink.common.constant.BusinessConstants;
import com.hanthink.common.constant.HttpStatus;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.core.page.TableDataInfo;
import com.hanthink.common.core.text.Convert;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.common.utils.SerialNumberUtil;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import com.hanthink.quartz.domain.*;
import com.hanthink.quartz.mapper.TaskMapper;
import com.hanthink.system.domain.SysSerialNumber;
import com.hanthink.system.domain.pc.EmTask;
import com.hanthink.system.domain.pc.EmTaskItem;
import com.hanthink.system.domain.pc.EmTaskPlan;
import com.hanthink.system.domain.report.*;
import com.hanthink.system.mapper.EmTaskMapper;
import com.hanthink.system.mapper.EmTaskPlanMapper;
import com.hanthink.system.mapper.SysSerialNumberMapper;
import com.hanthink.system.mapper.report.IndexMapper;
import com.hanthink.system.service.pc.IEmTaskItemService;
import com.hanthink.system.service.repair.IRepairRecordInfoService;
import com.hanthink.system.service.repair.IRequestFileInfoService;
import com.hanthink.system.service.report.IReportInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator 报表信息查询
 *
 */
@RestController
@RequestMapping("/liangpian")
public class ReportPlusControll extends BaseController {

	@Autowired
	private IReportInfoService reportInfoService;

	@Autowired
	private IndexMapper indexMapper;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private IRequestFileInfoService requestFileInfoService;

	@Autowired
	private IRepairRecordInfoService repairRecordInfoService;

	@Autowired
	private EmTaskPlanMapper emTaskPlanMapper;


	@Autowired
	private EmTaskMapper emTaskMapper;

	@Autowired
	private TaskMapper taskMapper;

	@Autowired
	private SysSerialNumberMapper sysSerialNumberMapper;

	@Autowired
	private IEmTaskItemService emTaskItemService;
	// 查询点检柱形图任务信息列表
	@PostMapping("/list")
	public TableDataInfo list(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.queryReportInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	@PostMapping("/listplan")
	public TableDataInfo listplan() {
		EmTaskPlan queryPlan = new EmTaskPlan();
		queryPlan.setHangameKey("622");
		queryPlan.setSiteKey("649");
		List<EmTaskPlan> plans = emTaskPlanMapper.selectEmTaskPlanListplan(queryPlan);

		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(plans);
		return tableDataInfo;
	}

	@PostMapping("/listplan1")
	public TableDataInfo listplan1() {
		List<EmTask> plans = emTaskMapper.selectEmTaskListhangzhou();

		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(plans);
		return tableDataInfo;
	}

	@PostMapping("/listplan2")
	public TableDataInfo listplan2(@RequestBody Map<String, Object> map) {

		String department = (String) map.get("department");
		String status = (String) map.get("status");
		String t1 = (String) map.get("t1");
		String t2 = (String) map.get("t2");

		List<EmTask> plans = emTaskMapper.selectEmTaskListhangzhouplus(department,status,t1,t2);

		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(plans);
		return tableDataInfo;
	}

	@PostMapping("/listplan3")
	public TableDataInfo listplan3(@RequestBody Map<String, Object> map) {

		String taskKey = (String) map.get("taskKey");
		List<EmTaskItem> list = emTaskItemService.selectEmTaskItemListplus(taskKey);
		return getDataTable(list);
	}

	@PostMapping("/listplan4")
	public TableDataInfo listplan4(@RequestBody Map<String, Object> map) {

		List<EmTaskItem> list = emTaskItemService.selectEmTaskItemListplus1();
		return getDataTable(list);
	}




	// 查询点检表格任务信息列表
	@PostMapping("/tableList")
	public TableDataInfo tableList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.querytableReportInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询工厂信息列表
	@PostMapping("/siteList")
	public TableDataInfo siteList() {
		SiteInfo info = new SiteInfo();
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getSite() != null) {
			info.setSiteKey(loginUser.getUser().getSite());
		}
		if (loginUser.getUser().getDept().getParentId() != 0L && loginUser.getUser().getHangame() != null) {
			info.setHangameKey(loginUser.getUser().getHangame());
		}
		List<SiteInfo> list = reportInfoService.querySiteInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询部门信息列表
	@PostMapping("/departList")
	public TableDataInfo departList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.queryDepartInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询未完成功能维修信息
	@PostMapping("/repairList")
	public TableDataInfo repairList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.queryRepairReportInfo(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	@PostMapping("/querynoRequestAndRepair")
	public TableDataInfo querynoRequestAndRepair(@RequestBody RequestAndRepairRecord requestAndRepairRecord) throws ParseException {
		List<RequestAndRepairRecordByTime> list = reportInfoService.queryRequestAndRepairbyTime(requestAndRepairRecord);
		List<RequestAndRepairRecordByTime> listnew = null;
		for (RequestAndRepairRecordByTime requestAndRepairRecordByTime : list) {
			RepairTimeRecord rt = new RepairTimeRecord();
			rt.setRepairId(Convert.toLong(requestAndRepairRecordByTime.getRepairId()));
			List<RepairTimeRecord> lsdt = reportInfoService.queryRepairTimeRecord(rt);
			for (RepairTimeRecord repairTimeRecord : lsdt) {
				requestAndRepairRecordByTime.setStartTime(repairTimeRecord.getStartTime());
				requestAndRepairRecordByTime.setEndTime(repairTimeRecord.getEndTime());
			}
		}
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("操作成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询具体信息
	@PostMapping("/queryList")
	@Log(title = "已完成工艺点检查询", businessType = BusinessType.OTHER)
	public TableDataInfo queryList(@RequestBody ReportInfo info) {
		List<ReportInfo> list = reportInfoService.queryList(info);
		TableDataInfo tableDataInfo = new TableDataInfo();
		tableDataInfo.setCode(HttpStatus.SUCCESS);
		tableDataInfo.setMsg("查询成功");
		tableDataInfo.setRows(list);
		return tableDataInfo;
	}

	// 查询我已完成的任务
	@PostMapping("/queryMyTaskList")
	public TableDataInfo queryMyTaskList(@RequestBody ReportInfo info) {
		startPage();
		// 获取当前登录用户
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		info.setExecuter(loginUser.getUser().getUserName());
		List<ReportInfo> list = reportInfoService.queryMyTaskList(info);
		return getDataTable(list);
	}

	// 查询首页看板数据
	@PostMapping("/indexData")
	public AjaxResult indexData(@RequestBody IndexInfo indexInfo) {
		AjaxResult ajax = AjaxResult.success();
		// 查询设备运行状态
		ajax.put("equipmentRunStatus", indexMapper.queryEquipmentStatus(indexInfo));
		// 查询今日报修
		ajax.put("repairOfToday", indexMapper.queryRepairOfToday(indexInfo));
		// 当月故障排行榜
		ajax.put("faultRankMonth", indexMapper.queryFaultRankMonth(indexInfo));
		// 点检任务统计
		ajax.put("pointTask", indexMapper.queryPointTask(indexInfo));
		// 待执行点检任务
		ajax.put("waitExecuteTask", indexMapper.queryWaitExecuteTask(indexInfo));
		// 自修统计
		ajax.put("selfRepair", indexMapper.querySelfRepair(indexInfo));
		return ajax;
	}

	// 查询报修和维修记录列表
	@PostMapping("/queryRequestAndRepair")
	public TableDataInfo queryRequestAndRepair(@RequestBody RequestAndRepairRecord requestAndRepairRecord) {
		startPage();
		List<RequestAndRepairRecord> list = reportInfoService.queryRequestAndRepair(requestAndRepairRecord);
		return getDataTable(list);
	}


	// 查询报修和维修记录详情
	@PostMapping("/queryRecordDetail")
	public AjaxResult queryRecordDetail(@RequestBody RequestAndRepairRecord requestAndRepairRecord) {
		AjaxResult ajax = AjaxResult.success();
		// 查询报修多媒体信息
		ajax.put("requestFiles", requestFileInfoService.SelectFileByRequestId(requestAndRepairRecord.getRequestId()));
		if (requestAndRepairRecord.getRepairId() != null) {
			// 查询维修多媒体信息
			ajax.put("repairFiles", requestFileInfoService.SelectRepairFileByRequestId(requestAndRepairRecord.getRepairId()));
			// 查询维修协助人
			ajax.put("heleUsers", repairRecordInfoService.selectHelpUserByRepairId(requestAndRepairRecord.getRepairId()));
			// 查询维修时间及工时
			RepairTimeRecord repairTimeRecord = new RepairTimeRecord();
			repairTimeRecord.setRepairId(requestAndRepairRecord.getRepairId());
			ajax.put("repairTime", reportInfoService.queryRepairTimeRecord(repairTimeRecord));
		}
		return ajax;
	}

	@PostMapping("/createtask")
	public AjaxResult createtask(@RequestBody Map<String, Object> map) {

		AjaxResult ajax = AjaxResult.success();
		List<EmPlanInfo> planTaskInfos = taskMapper.queryPlanTaskInfobycode(map.get("code").toString());

		if (planTaskInfos.size() > 0) {
			for (EmPlanInfo planTaskInfo : planTaskInfos) {

				String hangameKey = planTaskInfo.getHangameKey();
				String planCode = planTaskInfo.getPlanCode();
				String planName = planTaskInfo.getPlanName();
				String site = planTaskInfo.getSiteKey();
				String eqName = planTaskInfo.getEqKey();
				String eqStatus = planTaskInfo.getEqStatus();
				String department = planTaskInfo.getDepartment();
				Long cycle = planTaskInfo.getCycle();
				String startTime = planTaskInfo.getPlanStartTime();
				String endTime = planTaskInfo.getPlanEndTime();
				String nextTime = planTaskInfo.getNextExecuteTime();
				String type = planTaskInfo.getPlanType();
				String category = planTaskInfo.getPlanCategory();
				String isRepeat = planTaskInfo.getIsRepeat();
				String status = planTaskInfo.getStatus();
				String eid = planTaskInfo.getEid();
				Long id = planTaskInfo.getId();
				String target = planTaskInfo.getTarget();
				String estatus = planTaskInfo.getEstatus();
				String model = planTaskInfo.getModel();

				// 获取序列号
				SysSerialNumber serial = sysSerialNumberMapper.selectNextValue();
				String code = SerialNumberUtil.getSerialNumber(BusinessConstants.PLAN_TASK,
						serial.getNextValue(), serial.getToday());

				// 新增task
				TaskInfo task = new TaskInfo();
				task.setHangameKey(hangameKey);
				task.setTaskCode(code);
				task.setEqKey(Long.valueOf(eid));
				task.setTaskType(type);
				task.setTaskCategory(category);
				task.setStatus("0");
				task.setDepartment(department);
				task.setSiteKey(Long.valueOf(site));
				task.setEqStatus(eqStatus);
				task.setPlanId(String.valueOf(id));
				taskMapper.saveTask(task);

				EquipTaskItemInfo job = new EquipTaskItemInfo();
				job.setModel(model);
				job.setCycle(Integer.parseInt(cycle.toString()));
				job.setItemType(type);
				job.setDepartment(department);
				job.setEqKey(eqName);
				List<EquipTaskItemInfo> queryJobInfos = taskMapper.queryJobInfo(job);
				int i = 0;
				if (queryJobInfos.size() > 0) {
					for (EquipTaskItemInfo queryJobInfo : queryJobInfos) {
						// 根据点检项消息生成taskItem
						i++;
						TaskItemInfo itemInfo = new TaskItemInfo();
						itemInfo.setSiteKey(queryJobInfo.getSiteKey());
						itemInfo.setTaskKey(code);
						itemInfo.setItemCode(queryJobInfo.getItemCode());
						itemInfo.setItemName(queryJobInfo.getItemName());
						itemInfo.setItemDesc(queryJobInfo.getItemDesc());
						itemInfo.setItemType(queryJobInfo.getCategory());
						itemInfo.setExecuteMeans(queryJobInfo.getExecuteMeans());
						itemInfo.setOrderNum(i);
						itemInfo.setFilePath(queryJobInfo.getFilePath());
						taskMapper.saveTaskItem(itemInfo);

						// 点检项id 查询具体的选项任务
						Long jobId = queryJobInfo.getId();
						EmItemFileInfo fileInfo = new EmItemFileInfo();
						fileInfo.setItemId(jobId);
						List<EmItemFileInfo> selectEmItemFileLists = taskMapper
								.selectEmItemFileList(fileInfo);
						// 点检选项任务
						if (selectEmItemFileLists.size() > 0) {
							// 查询最新taskItem的ID信息
							Long itemId = taskMapper.queryTaskItemId().getId();
							for (EmItemFileInfo selectEmItemFileList : selectEmItemFileLists) {
								// 填充选项任务
								TaskSelectInfo taskSelectInfo = new TaskSelectInfo();
								taskSelectInfo.setItemId(itemId);
								taskSelectInfo.setSelectCode(selectEmItemFileList.getSelectCode());
								taskSelectInfo.setSelectDesc(selectEmItemFileList.getSelectDesc());
								taskSelectInfo.setFilePath(selectEmItemFileList.getFilePath());
								taskSelectInfo.setJobSelectId(selectEmItemFileList.getId());
								taskMapper.insertTaskSelect(taskSelectInfo);
							}
						}
					}
				}
			}
		}
		return ajax;
	}
}